package com.api.demo.service.impl;

import com.api.demo.entity.User;
import com.api.demo.entity.UserFollow;
import com.api.demo.mapper.UserFollowMapper;
import com.api.demo.mapper.UserMapper;
import com.api.demo.service.FollowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements FollowService {

    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean follow(Long followerId, Long followingId) {
        // 自己不能关注自己
        if (followerId.equals(followingId)) {
            return false;
        }

        // 检查用户是否存在
        User follower = userMapper.selectById(followerId);
        User following = userMapper.selectById(followingId);
        if (follower == null || following == null) {
            return false;
        }

        // 检查是否已关注
        if (isFollowing(followerId, followingId)) {
            return true;
        }

        // 创建关注关系
        UserFollow userFollow = new UserFollow();
        userFollow.setFollowerId(followerId);
        userFollow.setFollowingId(followingId);
        userFollow.setCreateTime(LocalDateTime.now());

        try {
            return save(userFollow);
        } catch (Exception e) {
            System.err.println("关注失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void notFollow(Long followerId, Long followingId) {

        QueryWrapper<UserFollow> wrapper = new QueryWrapper<UserFollow>();
        wrapper.eq("follower_id", followerId)
                .eq("following_id", followingId);
        UserFollow userFollow = userFollowMapper.selectOne(wrapper);
        removeById(userFollow.getId());

    }

    @Override
    public boolean unfollow(Long followerId, Long followingId) {
        try {
            QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("follower_id", followerId).eq("following_id", followingId);
            return remove(queryWrapper);
        } catch (Exception e) {
            System.err.println("取消关注失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean isFollowing(Long followerId, Long followingId) {
        QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", followerId).eq("following_id", followingId);
        return count(queryWrapper) > 0;
    }

    @Override
    public int getFollowingCount(Long userId) {
        return userFollowMapper.countFollowing(userId);
    }

    @Override
    public int getFollowersCount(Long userId) {
        return userFollowMapper.countFollowers(userId);
    }

    @Override
    public List<User> getFollowingUsers(Long userId) {
        return userFollowMapper.findFollowingUsers(userId);
    }

    @Override
    public List<User> getFollowers(Long userId) {
        return userFollowMapper.findFollowers(userId);
    }

    @Override
    public List<User> getFollowersWithPagination(Long userId, int offset, int limit) {
        return userFollowMapper.findFollowersWithPagination(userId, offset, limit);
    }

    @Override
    public List<User> getFollowingWithPagination(Long userId, int offset, int limit) {
        return userFollowMapper.findFollowingWithPagination(userId, offset, limit);
    }
}